﻿using System;
using System.Collections.Generic;

namespace School
{
    public class Course
    {
        public static readonly int MAX_STUDENTS_COUNT = 29;

        private Dictionary<int, Student> students;

        public Course()
        {
            this.students = new Dictionary<int, Student>();
        }

        public int StudentsCount
        {
            get
            {
                return this.students.Count;
            }
        }

        public Student this[int uniqueNumber]
        {
            get
            {
                if (!this.students.ContainsKey(uniqueNumber))
                {
                    throw new ArgumentException(string.Format("Student with unique number {0} does not exist.", uniqueNumber));
                }

                Student student = this.students[uniqueNumber];
                return student;
            }
        }

        public void Join(Student student)
        {
            this.Join(this.students, student);
        }

        public void Leave(Student student)
        {
            this.Leave(this.students, student);
        }

        private void Join(Dictionary<int, Student> students, Student student)
        {
            if (student == null)
            {
                throw new ArgumentNullException("Student can not be null.");
            }
            if (students.Count == MAX_STUDENTS_COUNT)
            {
                throw new Exception("The maximum students count in the course exceeded.");
            }
            if (students.ContainsKey(student.UniqueNumber))
            {
                throw new ArgumentException("Student already in the course.");
            }

            students.Add(student.UniqueNumber, student);
        }

        private void Leave(Dictionary<int, Student> students, Student student)
        {
            if (student == null)
            {
                throw new ArgumentNullException("The student can not be null.");
            }
            if (students.Count == 0)
            {
                throw new Exception("No students in the course.");
            }
            if (!students.ContainsKey(student.UniqueNumber))
            {
                throw new ArgumentException("The student does not exist.");
            }

            students.Remove(student.UniqueNumber);
        }
    }
}
